/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif
namespace Oceanside
{
    /// <summary>
    /// Interaction logic for PageLogin.xaml
    /// </summary>
    public partial class PageRootPasswordImp : PageImpBaseClass
    {
        /// <summary>
        /// Called when the user logs into this form.
        /// </summary>
        public delegate void BohResetDelegate();

        /// <summary>
        /// Called when the user presses the login button.
        /// </summary>
        private PageRootPassword.BohResetDelegate PasswordResetDelFunc = null;

        TextBox textBoxUsername;
        PasswordBox passwordBoxPassword;
        PasswordBox passwordBoxPasswordConfirm;
        TextBox textBoxEmail;
        TextBox textBoxQuestion;
        TextBox textBoxAnswer;
        Button buttonLogin;

        /// <summary>
        /// Constructor
        /// </summary>
        public PageRootPasswordImp(PageRootPassword.BohResetDelegate resetDel, 
            Button buttonLogin,
        TextBox textBoxUsername,
        PasswordBox passwordBoxPassword,
        PasswordBox passwordBoxPasswordConfirm,
        TextBox textBoxEmail,
        TextBox textBoxQuestion,
        TextBox textBoxAnswer)
        {
            this.buttonLogin = buttonLogin;
            this.textBoxUsername = textBoxUsername;
            this.passwordBoxPassword = passwordBoxPassword;
            this.passwordBoxPasswordConfirm = passwordBoxPasswordConfirm;
            this.textBoxAnswer = textBoxEmail;
            this.textBoxQuestion = textBoxQuestion;
            this.textBoxAnswer = textBoxAnswer;
            this.textBoxEmail = textBoxEmail;
            buttonLogin.Click += new RoutedEventHandler(buttonNext_Click_NOTHROW);
            PasswordResetDelFunc += resetDel;
        }

        /// <summary>
        /// 
        /// </summary>
        override public void ResetFocus()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        public void SetResetUsername(string user)
        {
            textBoxUsername.Text = user;
        }

        /// <summary>
        /// If the user presses the login button then perform a login.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void buttonNext_Click_NOTHROW(object sender, RoutedEventArgs e)
        {
            try
            {
                PerformReset();
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_PAGEROOTPASSWORD_NEXT_CLICKED, exc);
            }
        }

        /// <summary>
        /// If the user presses the enter key, then peform a login.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                PerformReset();
            }
        }

        /// <summary>
        /// The command function that actually performs the login.
        /// </summary>
        public void PerformReset()
        {
            if (DominicaEntities.SetBohPassword(textBoxUsername.Text, passwordBoxPassword.Password, passwordBoxPasswordConfirm.Password, textBoxEmail.Text, textBoxQuestion.Text, textBoxAnswer.Text))
            {
                if (PasswordResetDelFunc != null)
                {
                    PasswordResetDelFunc();
                }
            }
            else
            {
                System.Windows.MessageBox.Show("Either some fields in the form were blank, you did not enter a valid email address or you did not follow the password reset rules.  Please try again.", "Invalid Form Entries", MessageBoxButton.OK);
            }
        }
    }
}
